package com.supergo.manager.interceptor;

import com.supergo.manager.exception.AuthException;
import com.supergo.manager.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行权限拦截器");
        String authHeader = request.getHeader("Authorization");
        if(authHeader == null){
            throw new AuthException("暂无权限");
        }
        if(!authHeader.startsWith("Bearer")){
            throw new AuthException("暂无权限");
        }
        final String token = authHeader.substring(7);
        // 解析token
        Claims claims = jwtUtil.parseToken(token);
        // 执行具体授权业务......
        return true;
    }
}
